﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Security.Cryptography;

namespace SPPivotViewer.HttpHandler.Internal
{
    public class SPListCollectionParam
    {
        private string _strFacets;
        private string _strInfos;
        private string _strImageField;
        private string _strImageSubstitue;
        private string _strFieldUrl;
        private string _strKey;
        public string[] Facets
        {
            get{

                if (string.IsNullOrEmpty(_strFacets))
                    return null;
                return HttpUtility.UrlDecode(_strFacets).Split('|');
            }
        }

        public string[] Infos
        {
            get
            {
                if (string.IsNullOrEmpty(_strInfos))
                    return null;
                return HttpUtility.UrlDecode(_strInfos).Split('|');
            }
        }

        public string ImageField
        {
            get
            {
                return HttpUtility.UrlDecode(_strImageField);
            }
        }
        public string ImageSubstitue
        {
            get
            {
                return HttpUtility.UrlDecode(_strImageSubstitue);
            }
        }
        public string FieldUrl
        {
            get
            {
                return HttpUtility.UrlDecode(_strFieldUrl);
            }
        }
        public string Key
        {
            get
            {
                return _strKey;
            }
        }

        public SPListCollectionParam(HttpContext context)
        {
            HttpRequest request = context.Request;
            _strFacets = string.IsNullOrEmpty(request.Params["Facets"]) ? "" : HttpUtility.UrlDecode(request.Params["Facets"]).Replace(",","");
            _strImageField = string.IsNullOrEmpty(request.Params["ImageField"]) ? "" : HttpUtility.UrlDecode(request.Params["ImageField"]).Replace(",","");
            _strImageSubstitue = string.IsNullOrEmpty(request.Params["ImageSubstitue"]) ? "" : HttpUtility.UrlDecode(request.Params["ImageSubstitue"]).Replace(",", "");
            _strFieldUrl = string.IsNullOrEmpty(request.Params["FieldUrl"]) ? "" : HttpUtility.UrlDecode(request.Params["FieldUrl"]).Replace(",", "");
            _strInfos = string.IsNullOrEmpty(request.Params["Infos"]) ? "" : HttpUtility.UrlDecode(request.Params["Infos"]).Replace(",", "");
            _strKey = CreateMD5Hash(request.Url + _strFacets + _strImageField + _strImageSubstitue + _strFieldUrl + _strInfos);
        
        }
        public string CreateMD5Hash(string input)
        {
            MD5 md5 = MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hashBytes = md5.ComputeHash(inputBytes);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("x2"));
            }
            
            return sb.ToString();
        }

    }
}
